package com.domel76;

import java.util.List;

import com.domel76.quickfilm.beans.CinemaBean;
import com.domel76.quickfilm.beans.FilmBean;
import com.domel76.quickfilm.common.Constans;
import com.domel76.quickfilm.common.RSSParser;
import com.domel76.quickfilm.dao.CommonDAO;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.text.Html;
import android.text.Spanned;

public class MainView extends ListActivity {
	ProgressDialog myProgressDialog = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main);

		setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new String[] { "Seanse", "Filmy", "Kina" }));

		/*
		 * createDatabase();
		 * 
		 * RSSParser rssParser = new RSSParser(); List<FilmBean> films =
		 * rssParser.parse(); CommonDAO dao = new CommonDAO(this); for(FilmBean
		 * film : films) { int filmId = dao.getIdFilmByTitleAndInsert(film);
		 * System.out.println("FilmId:" + filmId); for(CinemaBean cinema :
		 * film.cinemas) { int cinemaId =
		 * dao.getIdCinemaByTitleAndInsert(cinema);
		 * System.out.println("CinemaId:" + filmId); for(String
		 * hour:cinema.hours) { dao.insertHour(filmId, cinemaId,hour); }
		 * 
		 * } }
		 */

		getListView().setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (arg2 == 0) {
					Intent intent = new Intent(getApplicationContext(), Seans.class);
					startActivityForResult(intent, 1337);
				} else if (arg2 == 1) {
					Intent intent = new Intent(getApplicationContext(), Film.class);
					startActivityForResult(intent, 1337);
				} else if (arg2 == 2) {
					Intent intent = new Intent(getApplicationContext(), Cinema.class);
					startActivityForResult(intent, 1337);
				}

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		MenuItem item = menu.add("Odśwież");
		item.setCheckable(true);
		item.setIcon(R.drawable.ic_menu_refresh);
		item.setOnMenuItemClickListener(myProgressBarShower);

		item = menu.add("Utwórz bazę");
		item.setCheckable(true);
		item.setIcon(android.R.drawable.ic_menu_add);
		item.setOnMenuItemClickListener(myCreateDatabase);

		return true;

	}

	public boolean onOptionsItemSelected(MenuItem item) {

		if (item.hasSubMenu() == false) {
		}
		return true;
	}

	OnMenuItemClickListener myCreateDatabase = new OnMenuItemClickListener() {
		@Override
		public boolean onMenuItemClick(MenuItem arg0) {
			createDatabase();
			return true;
		}

	};

	/** OnClickListener that fakes some work to be done. */
	OnMenuItemClickListener myProgressBarShower = new OnMenuItemClickListener() {
		@Override
		public boolean onMenuItemClick(MenuItem arg0) {
			// Display an indeterminate Progress-Dialog
			myProgressDialog = ProgressDialog.show(MainView.this, "Proszę czekać", "Odczytywanie nowej bazy filmów", true);

			new Thread() {
				public void run() {
					try {
						SQLiteDatabase baza = null;
						try {
							baza = openOrCreateDatabase(Constans.DB_NAME, MODE_PRIVATE, null);
							baza.execSQL("DELETE FROM Film");
							baza.execSQL("DELETE FROM Cinema");
							baza.execSQL("DELETE FROM Seans");

							RSSParser rssParser = new RSSParser();
							List<FilmBean> films = rssParser.parse();
							CommonDAO dao = new CommonDAO(baza);
							for (FilmBean film : films) {
								int filmId = dao.getIdFilmByTitleAndInsert(film);
								for (CinemaBean cinema : film.cinemas) {
									int cinemaId = dao.getIdCinemaByTitleAndInsert(cinema);
									for (String hour : cinema.hours) {
										dao.insertHour(filmId, cinemaId, hour);
									}
								}
							}

						} catch (SQLiteException e) {
							Log.e(getClass().getSimpleName(), "Could not create or Open the database");
						} finally {
							if (baza != null)
								baza.close();
						}
					} catch (Exception e) {
					}
					myProgressDialog.dismiss();
				}
			}.start();
			return false;
		}
	};

	private void createDatabase() {
		SQLiteDatabase baza = null;
		try {

			baza = this.openOrCreateDatabase(Constans.DB_NAME, MODE_PRIVATE, null);
			baza.execSQL("DROP TABLE IF EXISTS " + Constans.TABLE_FILM +";");
			baza.execSQL("DROP TABLE IF EXISTS " + Constans.TABLE_CINEMA +";");
			baza.execSQL("DROP TABLE IF EXISTS " + Constans.TABLE_SEANS +";");
			
			baza.execSQL("CREATE TABLE IF NOT EXISTS " + Constans.TABLE_FILM
					+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, title VARCHAR, description VARCHAR, runtime VARCHAR, details VARCHAR,  shortDescription VARCHAR, linkImage VARCHAR, link VARCHAR, rating VARCHAR, country VARCHAR, release VARCHAR, genre VARCHAR)");
			baza.execSQL("CREATE TABLE IF NOT EXISTS " + Constans.TABLE_CINEMA + " (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, address VARCHAR)");
			baza.execSQL("CREATE TABLE IF NOT EXISTS " + Constans.TABLE_SEANS
					+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, film_id INTEGER, cinema_id INTEGER, hour VARCHAR)");

			baza.execSQL("DELETE FROM Film");
			baza.execSQL("DELETE FROM Cinema");
			baza.execSQL("DELETE FROM Seans");

		} catch (SQLiteException e) {
			Log.e(getClass().getSimpleName(), "Could not create or Open the database");
		} finally {
			if (baza != null)
				baza.close();
		}
	}
}